package com.medical.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.medical.system.model.query.DictTypePageQuery;
import com.medical.core.result.PageResult;
import com.medical.core.result.Result;
import com.medical.system.model.form.DictForm;
import com.medical.system.model.form.DictTypeForm;
import com.medical.system.model.vo.DictTypePageVO;
import com.medical.system.model.vo.DictVO;
import com.medical.system.service.SysDictService;
import com.medical.system.service.SysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@Tag(name = "05.字典接口")
@RestController
@RequestMapping("/api/v1/dict")
public class SysDictController {

    private final SysDictService dictService;
    private final SysDictTypeService dictTypeService;

    @Operation(summary = "获取字典类型下字典列表")
    @GetMapping("/{typeCode}/list")
    public Result<List<DictVO>> getDictPage(@Parameter(description = "字典类型编码") @PathVariable String typeCode) {
        return Result.success(dictService.getDictList(typeCode));
    }

    @Operation(summary = "获取字典表单详情")
    @GetMapping("/{id}/form")
    public Result getDictForm(@Parameter(description = "字典ID") @PathVariable Long id) {
        return Result.success(dictService.getDictForm(id));
    }

    @Operation(summary = "保存字典")
    @PostMapping
    @SaCheckPermission("sys:dict:save")
    public Result addDict(@RequestBody DictForm dictForm) {
        return Result.judge(dictService.saveDict(dictForm));
    }

    @Operation(summary = "修改字典")
    @PutMapping("/{id}")
    @SaCheckPermission("sys:dict:update")
    public Result updateDict(@PathVariable  Long id, @RequestBody DictForm dictForm) {
        return Result.judge(dictService.updateDict(id, dictForm));
    }

    @Operation(summary = "删除字典")
    @DeleteMapping("/{idStr}")
    @SaCheckPermission("sys:dict:delete")
    public Result deleteDict(@Parameter(description = "字典ID字符串，多个英文逗号','分割") @PathVariable String idStr) {
        return Result.judge(dictService.deleteDict(idStr));
    }

    @Operation(summary = "获取字典下拉选项")
    @GetMapping("/options")
    public Result dictOptions(@Parameter(description = "字典类型代码") @RequestParam String typeCode) {
        return Result.success(dictService.listDictOptions(typeCode));
    }

    @Operation(summary = "获取字典类型分页")
    @GetMapping("/type/page")
    public PageResult<DictTypePageVO> getDictTypePage(@ParameterObject DictTypePageQuery pageQuery) {
        return PageResult.success(dictTypeService.getDictTypePage(pageQuery));
    }

    @Operation(summary = "获取字典类型表单详情")
    @GetMapping("/type/{id}/form")
    public Result getDictTypeForm(@Parameter(description = "字典类型ID") @PathVariable Long id) {
        return Result.success(dictTypeService.getDictTypeForm(id));
    }

    @Operation(summary = "添加字典类型")
    @PostMapping("/type")
    @SaCheckPermission("sys:dictType:add")
    public Result saveDictType(@RequestBody DictTypeForm dictTypeForm) {
        return Result.judge(dictTypeService.saveDictType(dictTypeForm));
    }

    @Operation(summary = "更新字典类型")
    @PutMapping("/type/{id}")
    @SaCheckPermission("sys:dictType:update")
    public Result updateDictType(@PathVariable  Long id, @RequestBody DictTypeForm dictTypeForm) {
        return Result.judge(dictTypeService.updateDictType(id, dictTypeForm));
    }
    
    @Operation(summary = "删除字典类型")
    @DeleteMapping("/type/{idStr}")
    @SaCheckPermission("sys:dictType:delete")
    public Result deleteDictType(@Parameter(description = "类型ID，多个以英文逗号（,）分割") @PathVariable  String idStr) {
        return Result.judge(dictTypeService.deleteDictType(idStr));
    }
}
